package com.xnx3.banmahuishou.superadmin.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.BaseVO;
import com.xnx3.DateUtil;
import com.xnx3.Lang;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 骑手管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouSuperadminQishouController")
@RequestMapping("/superadmin/qishou/")
public class QishouController extends BaseController {
	@Resource
	private SqlService sqlService;
	@Resource
	private UserService userService;
	
	
	/**
	 * 骑手列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"phone"});
		sql.appendWhere("(dailishang_userid IS NULL OR wangge_userid IS NULL)");
		int count = sqlService.count("laji_qishou", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_qishou", page);
		sql.setDefaultOrderBy("laji_qishou.id DESC");
		List<Qishou> list = sqlService.findBySql(sql, Qishou.class);
		
		ActionLogUtil.insert(request, "骑手管理", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/superadmin/qishou/list";
	}

	/**
	 * 新增/修改骑手
	 * @param id 要修改的id 。 如果是新增，这里传入0或者不传
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(
			@RequestParam(value = "id", required = false, defaultValue="0") int id,
			Model model, HttpServletRequest request){
		Qishou entity;
		
		if(id < 1){
			//新增
			entity = new Qishou();
			ActionLogUtil.insert(request, "进入骑手新增页面");
		}else{
			//修改
			entity = sqlService.findById(Qishou.class, id);
			if(entity == null){
				return error(model, "要修改的骑手不存在");
			}
			ActionLogUtil.insert(request, "进入修改骑手页面", entity.toString());
		}
		
		//编辑页面
		model.addAttribute("entity", entity);
		return "/superadmin/qishou/edit";
	}
	
	/**
	 * 新增、修改的骑手信息进行保存
	 */
	@RequestMapping(value="save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO variableSave(Qishou inputEntity, HttpServletRequest request){
		Qishou entity = null;			//骑手表
		
		if(inputEntity.getId() == null || inputEntity.getId() < 1){
			//新增
			entity = new Qishou();
			
			User user = new User();
			user.setUsername(Lang.uuid());
			user.setPassword(Lang.uuid());
			user.setNickname(inputEntity.getName());
			user.setPhone(inputEntity.getPhone());
			user.setHead(inputEntity.getHead());
			user.setAuthority("10");
			user.setRegtime(DateUtil.timeForUnix10());
			user.setAllSellWgight(0);
			user.setAllOrderNumbe(0);
			user.setAllIncomeMone(0);
			user.setMoney(0);
			user.setFreezemoney(0);
			BaseVO createUserVO = userService.createUser(user, request);
			if(createUserVO.getResult() - BaseVO.FAILURE == 0){
				return error(createUserVO.getInfo());
			}
			entity.setId(Lang.stringToInt(createUserVO.getInfo(), 0));
			ActionLogUtil.insertUpdateDatabase(request, user.getId(), "保存骑手信息", user.toString());
			//因为是新增，所以要加上新增时间
			entity.setAddtime(DateUtil.timeForUnix10());	
			entity.setAllServiceNumb(0);
			entity.setAllWgight(0);
			entity.setAllExpend(0);
			entity.setLongitude(119.14799);
			entity.setLatitude(36.711009);
		}else{
			//修改
			entity = sqlService.findById(Qishou.class, inputEntity.getId());
			if(entity == null){
				return error("要修改的目标不存在");
			}
		}
		if(!StringUtils.hasLength(inputEntity.getName())) {
			return error("请输入骑手姓名");
		}
		if(!StringUtils.hasLength(inputEntity.getPhone())) {
			return error("请输入手机号");
		}
		if(!StringUtils.hasLength(inputEntity.getAlipayAccount())) {
			return error("请输入支付宝账号");
		}
		if(!StringUtils.hasLength(inputEntity.getAlipayName())) {
			return error("请输入结算人姓名");
		}
		if(!StringUtils.hasLength(inputEntity.getHead())) {
			return error("请上传头像");
		}
		//保存
		entity.setName(inputEntity.getName());
		entity.setPhone(inputEntity.getPhone());
		entity.setAlipayAccount(inputEntity.getAlipayAccount());
		entity.setAlipayName(inputEntity.getAlipayName());
		entity.setHead(inputEntity.getHead());
		entity.setState(Qishou.STATE_NORMAL);
		
		sqlService.save(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存骑手信息", entity.toString());
		return success();
	}
	
	/**
	 * 删除骑手信息
	 */
	@RequestMapping(value="delete.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO delete(@RequestParam(value = "id", required = false, defaultValue="0") int id, HttpServletRequest request){
		Qishou entity = sqlService.findById(Qishou.class, id);
		User user = sqlService.findById(User.class, id);
		if(user == null){
			return error("要删除的目标不存在");
		}
		sqlService.delete(entity);
		sqlService.delete(user);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "删除骑手", entity.toString());
		ActionLogUtil.insertUpdateDatabase(request, user.getId(), "删除骑手", user.toString());
		return success();
	}
	
	/**
	 * 设置工作状态
	 * @param id id编号
	 */
	@RequestMapping(value="stateEdit.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO stateEdit(HttpServletRequest request,
			@RequestParam(value = "id", required = false, defaultValue="0") int id){	
		
		Qishou qishou = sqlService.findById(Qishou.class, id);
		if(qishou == null){
			return error("要设置状态的目标不存在");
		}
		//设置请假
		if (qishou.getState() - 1 == 0) {
			qishou.setState(Qishou.STATE_LEAVE);
		}else {
			// 设置正常
			qishou.setState(Qishou.STATE_NORMAL);
		}
		sqlService.save(qishou);
		ActionLogUtil.insertUpdateDatabase(request, "正常/请假","Id为" + id + "的骑手，操作正常/请假");
		return success();
	}
	
	
	/**
	 * 骑手(v2)列表
	 */
	@RequestMapping("listv2${url.suffix}")
	public String listv2(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"phone","state=","dailishang_username","wangge_username"});
		sql.appendWhere("(dailishang_userid IS NOT NULL AND wangge_userid IS NOT NULL)");
		int count = sqlService.count("laji_qishou", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_qishou", page);
		sql.setDefaultOrderBy("laji_qishou.id DESC");
		List<Qishou> list = sqlService.findBySql(sql, Qishou.class);
		
		ActionLogUtil.insert(request, "骑手管理", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/superadmin/qishou/listv2";
	}
}
